/**
 * 
 */
package com.swim.shop.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.swim.shop.entity.Product;
import com.swim.natatoriumgoods.entity.NatatoriumGoods;
import com.swim.shop.dao.ProductDao;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 产品Service
 * @author ljw
 * @version 2023-05-16
 */
@Service
@Transactional(readOnly=true)
public class ProductService extends CrudService<ProductDao, Product> {
	
	@Value("${server.context-path}")
	private String contextPath;
	
	/**
	 * 获取单条数据
	 * @param product
	 * @return
	 */
	@Override
	public Product get(Product product) {
		Product result = super.get(product);
		if (result != null) {
			//获取产品封面图片url
			List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(result.getId(), "product_image");
			if (!CollectionUtils.isEmpty(fileUploadListOne)) {
				result.setProductImageUrl(contextPath + "/userfiles/fileupload/" + fileUploadListOne.get(0).getFileEntity().getFilePath() + fileUploadListOne.get(0).getFileEntity().getFileId() + "." + fileUploadListOne.get(0).getFileEntity().getFileExtension());
			}
		}
		return result;
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param product
	 * @return
	 */
	@Override
	public Page<Product> findPage(Page<Product> page, Product product) {
		Page<Product> pageList = super.findPage(page, product);
		List<Product> productList = pageList.getList();
		if (!CollectionUtils.isEmpty(productList)) {
			for (Product productOne : productList) {
				//获取产品封面图片url
				List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(productOne.getId(), "product_image");
				if (!CollectionUtils.isEmpty(fileUploadListOne)) {
					productOne.setProductImageUrl(contextPath + "/userfiles/fileupload/" + fileUploadListOne.get(0).getFileEntity().getFilePath() + fileUploadListOne.get(0).getFileEntity().getFileId() + "." + fileUploadListOne.get(0).getFileEntity().getFileExtension());
				}
			}
		}
		pageList.setList(productList);
		return pageList;
	
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param product
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Product product) {
		super.save(product);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(product.getId(), "product_image");
	}
	
	/**
	 * 更新状态
	 * @param product
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Product product) {
		super.updateStatus(product);
	}
	
	/**
	 * 删除数据
	 * @param product
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Product product) {
		super.delete(product);
	}
	
}